package com.tg.app.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.tg.app.R;
import com.tg.appcommon.android.CruiseBottomDialog;
import com.tg.appcommon.android.ResourcesUtil;

public class PrePositionListDialog {
    public static final int MODE_LAMP = 999;
    private final Context mContext;
    private Dialog mDialog;
    private int mode;
    private int position = -1;

    private PrePositionListDialogListener listener;

    private TextView tv1, tv2, tv3;

    public void setListener(PrePositionListDialogListener  listener) {
        this.listener = listener;
    }

    public PrePositionListDialog(@NonNull Context context) {
        this.mContext = context;
    }

    @NonNull
    public PrePositionListDialog builder(int position, int mode) {
        this.mode = mode;
        this.position = position;
        int resource = R.layout.dialog_pre_position_list;
        if (mode == MODE_LAMP){
            resource = R.layout.dialog_pre_position_lamp;
        }
        @SuppressLint("InflateParams") View view = LayoutInflater.from(mContext).inflate(resource, null);
        tv1 = view.findViewById(R.id.tv1);
        tv2 = view.findViewById(R.id.tv2);
        tv3 = view.findViewById(R.id.tv3);
        if (mode == 3){
            tv1.setText(R.string.stop_cruise);
            tv2.setVisibility(View.GONE);
            tv3.setText(R.string.txt_stop);
        }else if (mode == 4){
            tv2.setVisibility(View.GONE);
            tv1.setText(R.string.switch_cruise);
            tv3.setText(R.string.txt_switch);
        } else if (mode != MODE_LAMP){
            String title = String.format(ResourcesUtil.getString(R.string.pre_position_setting), position + 1);
            tv1.setText(title);
        }


        if (mode == 0){
            tv2.setVisibility(View.GONE);
            tv3.setText(R.string.pre_position_setting_pos);
        }else if (mode == 2){
            tv3.setText(R.string.remove_pre_position_setting_pos);
            tv3.setTextColor(ResourcesUtil.getResources().getColor(R.color.lock_dell_no_call_color));
        }

        tv2.setOnClickListener(view1 -> {
            if (listener != null){
                listener.onClick(view1, PrePositionListDialog.this.position, PrePositionListDialog.this.mode);
            }
            mDialog.dismiss();
        });

        tv3.setOnClickListener(view12 -> {
            if (listener != null){
                listener.onClick(view12, PrePositionListDialog.this.position, PrePositionListDialog.this.mode);
            }
            mDialog.dismiss();
        });

        if (mode == MODE_LAMP){
            mDialog = new Dialog(mContext, R.style.alert_dialog);
        }else {
            mDialog = new CruiseBottomDialog(mContext, R.style.alert_dialog);
        }

        mDialog = new CruiseBottomDialog(mContext, R.style.alert_dialog);
        mDialog.setContentView(view);
        view.findViewById(R.id.tv4).setOnClickListener(v -> mDialog.dismiss());

        LinearLayout layout = view.findViewById(R.id.lLayout_bg);
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        layout.setLayoutParams(new FrameLayout.LayoutParams((int) (displayMetrics.widthPixels * 0.80), LinearLayout.LayoutParams.WRAP_CONTENT));

        return this;
    }

    public void show() {
        if (mContext instanceof Activity) {
            if (((Activity) mContext).isFinishing()) return;
        }

        mDialog.show();
    }

    public interface PrePositionListDialogListener {
        void onClick(View view, int position, int mode);
    }
}
